from rest_framework import viewsets
from users.models import User, Dept
from django.contrib.auth.models import Group, Permission
from users.serializers import UserSerializer, DeptSerializer, GroupSerializer, PermissionSerializer
from mes_api.utils.base_viewsets import BaseViewSet
from users.permissions import IsHasPermission

# Create your views here.
# 基于基类创建用户视图集
class UserViewSet(BaseViewSet):
    # 过滤字段
    filter_fields = ('status',)
    # 查询字段
    search_fields = ('name', 'id', 'mobile', 'email')
    # 排序字段
    ordering_fields = ('dept', 'groups', 'last_login', 'update_time', 'create_time',
                       'mobile', 'email', 'status', 'id')
    # 查询集
    queryset = User.objects.all()
    # 序列化类
    serializer_class = UserSerializer
    # 权限类
    permission_classes = (IsHasPermission,)


# 参照用户视图集创建部门视图集
class DeptViewSet(BaseViewSet):
    # 过滤字段
    filter_fields = ('status',)
    # 查询字段
    search_fields = ('name', 'id', 'title')
    # 排序字段
    ordering_fields = ('name', 'id', 'title', 'status',
                       'update_time', 'create_time')
    # 查询集
    queryset = Dept.objects.all()
    # 序列化类
    serializer_class = DeptSerializer

# 参照用户视图集创建组视图集
class GroupViewSet(BaseViewSet):
    # 过滤字段
    filter_fields = ('permissions__codename',)
    # 查询字段
    search_fields = ('name', 'id')
    # 排序字段
    ordering_fields = ('name', 'id')
    # 查询集
    queryset = Group.objects.all()
    # 序列化类
    serializer_class = GroupSerializer

# 参照用户视图集创建权限视图集
class PermissionViewSet(BaseViewSet):
    # 查询字段
    search_fields = ('name', 'id')
    # 排序字段
    ordering_fields = ('name', 'id')
    # 查询集
    queryset = Permission.objects.all()
    # 序列化类
    serializer_class = PermissionSerializer